const express = require('express');
      router = express.Router();


router.get('/back',(req,res) =>{
	send(res,'success','666')
})

router.get('/',(req,res) => {
   var page=req.query.page || 1
   page=parseInt(page)
   var countOfPage=5
   Ask.find(function(err,data){
         data.forEach(function(ele,num){
            var asId = ele.id
            Answer.find({askId:asId}).populate('userId').exec(function(err,ans){
//          	    console.log(ans)
                  Ask.update({_id:asId},{ans:ans}).exec(function(err,dat){
//                	console.log(dat)
                  })
            })
         })
  })
  setTimeout(function() {
      Ask.count().exec(function(err,total){
         var count=Math.ceil(total/countOfPage)
         var pages=[]
         for(var i=0;i<count;i++){
               pages[i]=i+1
            
         }
         console.log((page-1)*5)
         Ask.find().skip().limit(countOfPage).sort({time:-1}).populate('userId').exec(function(err,data){
//          console.log(data)
            res.render('index',{
                  question:data,
                  title:'提问',
                  page,
                  count,
                  countOfPage,
                  pages
            })
        })
     })
  }, 50);
  
})


module.exports = router
